package com.quanquan.management.controller;

import base.BaseConst;
import com.quanquan.management.dto.CouponDTO;
import com.quanquan.management.dto.ShoppingKillDTO;
import com.quanquan.management.request.ShoppingKillRequestBody;
import com.quanquan.management.service.CouponService;
import com.quanquan.management.service.ShoppingKillService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import web.response.ResultBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:
 *
 * @ClassName: ShoppingKillController
 * @Author: xiaoqianh丶
 * @Date: 2019-07-11 10:34
 * @Version: V1.0
 */
@Controller
@RequestMapping("/shopping_kill")
public class ShoppingKillController extends ManagerBaseController {

    @Autowired
    private ShoppingKillService shoppingKillService;

    @Autowired
    private CouponService couponService;

    @GetMapping("/list")
    public String list() {
        return "shoppingKill/list";
    }

    /**
     * 添加
     * @param requestBody
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String add(ShoppingKillRequestBody requestBody, Model model) {

        List<CouponDTO> couponDTOS = couponService.couponList();
        model.addAttribute("couponDTOS", couponDTOS);
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            return "shoppingKill/form";
        }
        boolean add =shoppingKillService.addShoppingKill(requestBody);
        if (add) {
            return "redirect:/shopping_kill/list";
        } else {
            Map<String, Object> result = new HashMap<>();
            result.put("code",-1);
            result.put("msg","添加失败");
            model.addAllAttributes(result);
            return null;
        }
    }

    @GetMapping("/getInventory")
    @ResponseBody
    public ResultBody getByMobile(@RequestParam String couponId) {
        CouponDTO byId = couponService.findById(couponId);
        if (null != byId) {
                return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(),byId);
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
    }
    /**
     * 编辑
     * @param id
     * @param requestBody
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(String id, ShoppingKillRequestBody requestBody, Model model) {
        List<CouponDTO> couponDTOS = couponService.couponList();
        model.addAttribute("couponDTOS", couponDTOS);
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            ShoppingKillDTO byId = shoppingKillService.findById(id);
            model.addAttribute("editable",byId);
            return "shoppingKill/form";
        }
        System.out.println("sdasdsd....ssds");
        requestBody.setShoppingKillId(id);
        requestBody.setInventoryAdjustment(0);
        boolean update = shoppingKillService.update(requestBody);
        if (update) {
            return "redirect:/shopping_kill/list";
        } else {
            model.addAttribute("errorCode",-1);
            model.addAttribute("errorMsg","修改失败,秒杀商品原有库存不足");
            return "shoppingKill/form";
        }
    }



    /**
     * 编辑
     * @param id
     * @param requestBody
     * @param model
     * @return
     */
    @RequestMapping("/edit_inventory")
    public String editInventory(String id, ShoppingKillRequestBody requestBody, Model model) {
        List<CouponDTO> couponDTOS = couponService.couponList();
        model.addAttribute("couponDTOS", couponDTOS);
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            ShoppingKillDTO byId = shoppingKillService.findById(id);
            model.addAttribute("editable",byId);
            return "shoppingKill/updateInventory";
        }
        requestBody.setShoppingKillId(id);
        requestBody.setInventoryAdjustment(1);
        boolean update = shoppingKillService.update(requestBody);
        if (update) {
            return "redirect:/shopping_kill/list";
        } else {
            model.addAttribute("errorCode",-1);
            model.addAttribute("errorMsg","修改失败,秒杀商品原有库存不足");
            return "shoppingKill/updateInventory";
        }
    }
}
